<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- CSRF元数据 -->
    <meta name="_csrf" th:content="${_csrf.token}"/>
    <meta name="_csrf_header" th:content="${_csrf.headerName}"/>
    <meta name="_csrf_parameter" th:content="${_csrf.parameterName}"/>
    <title>库存管理 - 哈尔滨双城区农产品物流园管理系统</title>
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/5.15.3/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/datatables/1.10.21/css/dataTables.bootstrap4.min.css">
    <link rel="stylesheet" th:href="@{/css/style.css}">
    <style>
        .content-header {
            margin-bottom: 20px;
        }
        .stock-container {
            padding: 20px;
        }
        .action-btn {
            margin-right: 5px;
        }
        .dataTables_wrapper {
            padding: 0;
        }
        .form-group.required .control-label:after {
            content: "*";
            color: red;
        }
        .modal-footer {
            justify-content: space-between;
        }
        .inventory-low {
            color: #dc3545;
            font-weight: bold;
        }
        .inventory-medium {
            color: #ffc107;
            font-weight: bold;
        }
        .inventory-high {
            color: #28a745;
        }
    </style>
</head>
<body>
    <div class="stock-container">
        <!-- 内容头部 -->
        <div class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1 class="m-0"><i class="fas fa-warehouse mr-2"></i>库存管理</h1>
                    </div>
                    <div class="col-sm-6">
                        <ol class="breadcrumb float-sm-right">
                            <li class="breadcrumb-item"><a href="#" onclick="parent.loadContent('dashboard')">首页</a></li>
                            <li class="breadcrumb-item active">库存管理</li>
                        </ol>
                    </div>
                </div>
            </div>
        </div>

        <!-- 主要内容 -->
        <div class="container-fluid">
            <!-- 库存统计卡片 -->
            <div class="row mb-4">
                <div class="col-md-3">
                    <div class="card bg-info">
                        <div class="card-body">
                            <h5 class="card-title">总库存数量</h5>
                            <p class="card-text h2" id="totalStockCount">加载中...</p>
                        </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card bg-success">
                        <div class="card-body">
                            <h5 class="card-title">总库存价值</h5>
                            <p class="card-text h2" id="totalStockValue">加载中...</p>
                        </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card bg-warning">
                        <div class="card-body">
                            <h5 class="card-title">库存预警</h5>
                            <p class="card-text h2" id="lowStockCount">加载中...</p>
                        </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card bg-danger">
                        <div class="card-body">
                            <h5 class="card-title">缺货商品</h5>
                            <p class="card-text h2" id="outOfStockCount">加载中...</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 卡片 -->
            <div class="card">
                <div class="card-header">
                    <div class="row">
                        <div class="col-md-8">
                            <h3 class="card-title">库存信息列表</h3>
                        </div>
                        <div class="col-md-4 text-right">
                            <button type="button" class="btn btn-primary" onclick="addStock()">
                                <i class="fas fa-plus"></i> 入库登记
                            </button>
                            <button type="button" class="btn btn-warning ml-2" onclick="exportStockData()">
                                <i class="fas fa-file-export"></i> 导出
                            </button>
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    <!-- 搜索表单 -->
                    <div class="row mb-3">
                        <div class="col-md-12">
                            <form id="searchForm" class="form-inline">
                                <div class="form-group mb-2 mr-3">
                                    <label for="searchProductName" class="mr-2">产品名称:</label>
                                    <input type="text" class="form-control" id="searchProductName" placeholder="输入产品名称">
                                </div>
                                <div class="form-group mb-2 mr-3">
                                    <label for="searchWarehouse" class="mr-2">仓库:</label>
                                    <select class="form-control" id="searchWarehouse">
                                        <option value="">全部</option>
                                        <!-- 仓库选项将通过AJAX加载 -->
                                    </select>
                                </div>
                                <div class="form-group mb-2 mr-3">
                                    <label for="searchStatus" class="mr-2">状态:</label>
                                    <select class="form-control" id="searchStatus">
                                        <option value="">全部</option>
                                        <option value="normal">正常</option>
                                        <option value="low">库存预警</option>
                                        <option value="out">缺货</option>
                                    </select>
                                </div>
                                <button type="button" class="btn btn-primary mb-2 mr-2" onclick="searchStock()">
                                    <i class="fas fa-search"></i> 搜索
                                </button>
                                <button type="button" class="btn btn-secondary mb-2" onclick="resetSearch()">
                                    <i class="fas fa-sync-alt"></i> 重置
                                </button>
                            </form>
                        </div>
                    </div>

                    <!-- 数据表格 -->
                    <div class="table-responsive">
                        <table id="stockTable" class="table table-bordered table-hover">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>产品名称</th>
                                    <th>产品编号</th>
                                    <th>仓库</th>
                                    <th>库存数量</th>
                                    <th>单位</th>
                                    <th>库存价值</th>
                                    <th>最后入库时间</th>
                                    <th>最后出库时间</th>
                                    <th>库存状态</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <!-- 数据将通过Ajax加载 -->
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 入库/编辑库存模态框 -->
    <div class="modal fade" id="stockModal" tabindex="-1" role="dialog" aria-labelledby="stockModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="stockModalLabel">入库登记</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form id="stockForm">
                        <input type="hidden" id="stockId" name="stockId">
                        <div class="form-row">
                            <div class="form-group required col-md-6">
                                <label for="productId" class="control-label">产品:</label>
                                <select class="form-control" id="productId" name="productId" required>
                                    <option value="">请选择产品</option>
                                    <!-- 产品选项将通过AJAX加载 -->
                                </select>
                            </div>
                            <div class="form-group required col-md-6">
                                <label for="warehouseId" class="control-label">仓库:</label>
                                <select class="form-control" id="warehouseId" name="warehouseId" required>
                                    <option value="">请选择仓库</option>
                                    <!-- 仓库选项将通过AJAX加载 -->
                                </select>
                            </div>
                        </div>
                        <div class="form-row">
                            <div class="form-group required col-md-6">
                                <label for="quantity" class="control-label">库存数量:</label>
                                <input type="number" class="form-control" id="quantity" name="quantity" min="0" required>
                            </div>
                            <div class="form-group col-md-6">
                                <label for="unit">单位:</label>
                                <input type="text" class="form-control" id="unit" name="unit" readonly>
                            </div>
                        </div>
                        <div class="form-row">
                            <div class="form-group col-md-6">
                                <label for="price">单价(元):</label>
                                <input type="number" class="form-control" id="price" name="price" min="0" step="0.01" readonly>
                            </div>
                            <div class="form-group col-md-6">
                                <label for="totalValue">库存价值(元):</label>
                                <input type="text" class="form-control" id="totalValue" name="totalValue" readonly>
                            </div>
                        </div>
                        <div class="form-row">
                            <div class="form-group col-md-6">
                                <label for="warningQuantity">预警库存:</label>
                                <input type="number" class="form-control" id="warningQuantity" name="warningQuantity" min="0">
                                <small class="form-text text-muted">低于此值将显示预警状态</small>
                            </div>
                            <div class="form-group col-md-6">
                                <label for="locationCode">存放位置:</label>
                                <input type="text" class="form-control" id="locationCode" name="locationCode" placeholder="如：A区-01-01">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="remark">备注:</label>
                            <textarea class="form-control" id="remark" name="remark" rows="2"></textarea>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" id="deleteBtn" onclick="confirmDelete()" style="display:none;">
                        <i class="fas fa-trash-alt"></i> 删除
                    </button>
                    <div>
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                        <button type="button" class="btn btn-primary" onclick="saveStock()">保存</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 出库操作模态框 -->
    <div class="modal fade" id="outStockModal" tabindex="-1" role="dialog" aria-labelledby="outStockModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="outStockModalLabel">出库操作</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form id="outStockForm">
                        <input type="hidden" id="outStockId" name="stockId">
                        <div class="form-group">
                            <label for="productName">产品名称:</label>
                            <input type="text" class="form-control" id="outProductName" readonly>
                        </div>
                        <div class="form-group">
                            <label for="warehouseName">仓库:</label>
                            <input type="text" class="form-control" id="outWarehouseName" readonly>
                        </div>
                        <div class="form-row">
                            <div class="form-group col-md-6">
                                <label for="currentQuantity">当前库存:</label>
                                <input type="text" class="form-control" id="currentQuantity" readonly>
                            </div>
                            <div class="form-group col-md-6">
                                <label for="outUnit">单位:</label>
                                <input type="text" class="form-control" id="outUnit" readonly>
                            </div>
                        </div>
                        <div class="form-group required">
                            <label for="outQuantity" class="control-label">出库数量:</label>
                            <input type="number" class="form-control" id="outQuantity" name="outQuantity" min="1" required>
                            <small id="outQuantityHelp" class="form-text text-danger"></small>
                        </div>
                        <div class="form-group">
                            <label for="outReason">出库原因:</label>
                            <select class="form-control" id="outReason" name="outReason">
                                <option value="sale">销售</option>
                                <option value="transfer">调拨</option>
                                <option value="damage">损坏</option>
                                <option value="expire">过期</option>
                                <option value="other">其他</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="outRemark">备注:</label>
                            <textarea class="form-control" id="outRemark" name="outRemark" rows="2"></textarea>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="confirmOutStock">确认出库</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 确认删除模态框 -->
    <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="deleteModalLabel">确认删除</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <p>确定要删除这条库存记录吗？此操作不可恢复。</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-danger" onclick="deleteStock()">确认删除</button>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/popper.js/1.16.1/umd/popper.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/datatables/1.10.21/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/datatables/1.10.21/js/dataTables.bootstrap4.min.js"></script>
    <script th:src="@{/js/stock.js}"></script>
</body>
</html> 